using KinonekoSoftware.Studio.Models.Theories;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    partial class WorldViewConfigTheoryViewModel
    {

        //-------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------

        private async void DoNewElementCommand()
        {
            var r = await NewElementViewModel.Create();

            if (!r.IsFinished)
            {
                return;
            }

            var val = r.Value;

            //
            //
            _theory_element.Add(val);

            //
            //
            Elements.Add(TheoryOfElementPresenter.Select(val, Placement));

            //
            Save();
        }

        private async void DoResetElementCommand()
        {
            if (!await DangerOperation(TitleSR.DangerOperation, TextSR.Remove_Item_All))
            {
                return;
            }
            
            
            
            Elements.Clear();
            _theory_element.Clear();
            //
            Save();
        }

        private async void DoEditElementCommand(TheoryOfElementPresenter element)
        {
            if (element is null)
            {
                return;
            }

            var r = await NewElementViewModel.Edit(element.TargetObject);

            if (!r.IsFinished)
            {
                return;
            }

            //
            //
            Save();

            //
            //
            element.RaiseUpdates();
            _theory_element.Update(element.TargetObject);
        }

        private async void DoRemoveElementCommand(TheoryOfElementPresenter element)
        {
            if (element is null)
            {
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            Elements.Remove(element);
            _theory_element.Remove(element.TargetObject);

            //
            //
            Save();
        }

        //-------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------

        public bool Placement
        {
            get => _placement;
            set
            {
                SetValue(ref _placement, value);
                _theory_element.ImagePlacement = value;
                Elements.ForEach(x => x.SetPlacement(Placement));
                Save();
            }
        }

        public ICommandEX NewElementCommand    { get; }
        public ICommandEX EditElementCommand   { get; }
        public ICommandEX ResetElementCommand  { get; }
        public ICommandEX RemoveElementCommand { get; }

        public ObservableCollection<TheoryOfElementPresenter> Elements { get; }
    }
}